package com.ruoyi.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.GenConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysTableColumn;
import com.ruoyi.system.mapper.SysTableColumnMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysTableMapper;
import com.ruoyi.system.domain.SysTable;
import com.ruoyi.system.service.ISysTableService;

/**
 * 表扩展Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-25
 */
@Service
public class SysTableServiceImpl extends ServiceImpl<SysTableMapper, SysTable> implements ISysTableService {
    public static String SYSTEM_COLUMN = "id|create_id|create_by|create_time|update_id|update_by|update_time";
    @Autowired
    private SysTableMapper sysTableMapper;
    @Autowired
    private SysTableColumnMapper sysTableColumnMapper;

    /**
     * 查询表扩展
     * 
     * @param id 表扩展主键
     * @return 表扩展
     */
    @Override
    public SysTable selectSysTableById(Long id)
    {
        return sysTableMapper.selectSysTableById(id);
    }

    /**
     * 查询表扩展列表
     * 
     * @param sysTable 表扩展
     * @return 表扩展
     */
    @Override
    public List<SysTable> selectSysTableList(SysTable sysTable)
    {
        return sysTableMapper.selectSysTableList(sysTable);
    }

    /**
     * 新增表扩展
     * 
     * @param sysTable 表扩展
     * @return 结果
     */
    @Override
    public int insertSysTable(SysTable sysTable)
    {
        return sysTableMapper.insertSysTable(sysTable);
    }

    /**
     * 修改表扩展
     * 
     * @param sysTable 表扩展
     * @return 结果
     */
    @Override
    public int updateSysTable(SysTable sysTable)
    {
        return sysTableMapper.updateSysTable(sysTable);
    }

    /**
     * 批量删除表扩展
     * 
     * @param ids 需要删除的表扩展主键
     * @return 结果
     */
    @Override
    public int deleteSysTableByIds(Long[] ids)
    {
        int num =  sysTableMapper.deleteSysTableByIds(ids);
        sysTableColumnMapper.deleteSysTableColumnByTableIds(ids);
        return num;
    }

    /**
     * 删除表扩展信息
     * 
     * @param id 表扩展主键
     * @return 结果
     */
    @Override
    public int deleteSysTableById(Long id)
    {
        return sysTableMapper.deleteSysTableById(id);
    }

    @Override
    public List<SysTable> selectDbList(SysTable sysTable) {
        return sysTableMapper.selectDbList(sysTable);
    }

    @Override
    public List<SysTable> selectDbTableListByNames(String[] tableNames) {
        return sysTableMapper.selectDbTableListByNames(tableNames);
    }

    @Override
    public void importTable(List<SysTable> tableList, String username) {
        try {
            for(SysTable table : tableList) {
                String tableName = table.getTableName();
                table.setCreateBy(username);

                super.save(table);
                // int row = sysTableMapper.insertSysTable(table);
                List<SysTableColumn> columns = sysTableColumnMapper.selectDbTableColumnsByName(tableName);
                for(SysTableColumn column : columns) {
                    String columnName = column.getColumnName();
                    column.setTableId(table.getId());
                    column.setCreateBy(table.getCreateBy());
                    column.setQueryType(GenConstants.QUERY_EQ);
                    column.setLen(column.getLen());

                    if(isSystemColumn(column)) {
                        column.setIsInsert("1");
                    }

                    sysTableColumnMapper.insertSysTableColumn(column);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("导入表失败：" + e.getMessage());
        }
    }

    private boolean isSystemColumn(SysTableColumn column) {
        String columnName = column.getColumnName();
        String[] keywords = StringUtils.split(SYSTEM_COLUMN, "\\|");

        for(String keyword : keywords) {
            if(StringUtils.indexOfIgnoreCase(columnName, keyword) > -1) {
                return true;
            }
        }
        return false;
    }
}
